#include <iostream>
#include <map>

class Solution {
public:
    int romanToInt(string s) {
        map<char, int> SymbolValue{{'I', 1},
                                   {'V', 5},
                                   {'X', 10},
                                   {'L', 50},
                                   {'C', 100},
                                   {'D', 500},
                                   {'M', 1000}};
        int ans = 0;
        char p = 0;
        for (char ch : s)
        {
            ans += SymbolValue[ch];
            if (p && SymbolValue[ch] > SymbolValue[p])
            {
              ans -= 2*SymbolValue[p];
            }
            p = ch;
        }
        return ans; 
    }
};